<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\TopicsDraft;
use App\Constants\TagConstant;
use App\Models\Tag;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TopicsDraftController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TopicsDraft(['member', 'topicTag']), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map))->orderByDesc('id');

            $grid->column('id');
            $grid->column('member.mini_type', '小程序')->display(function ($type) use ($map) {
                return $map[$type] ?? $type;
            });
            $grid->column('member.nickname', '用户');
            $grid->column('topicTag.name', '标签');
            $grid->column('themes', '话题');
            $grid->column('title');
            $grid->column('content')->limit(20, '...');
            $grid->column('status');
            $grid->column('mini_type');
            $grid->column('publish_date');
            $grid->column('status')->using(['未发布', '已发布'])->label(['danger', 'success']);
            $grid->column('created_at');

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableViewButton();
            $grid->disableRowSelector();

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('status')->select(['未发布', '已发布'])->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TopicsDraft(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('title');
            $show->field('content');
            $show->field('files');
            $show->field('relay_count');
            $show->field('fav_count');
            $show->field('is_hot');
            $show->field('status');
            $show->field('tag_id');
            $show->field('mini_type');
            $show->field('publish_date');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TopicsDraft(), function (Form $form) {
            $form->display('id');
            $form->select('member_id', '用户')->options(member_options())->required()->customFormat(function () {
                return sprintf('%s|%s', $this->member_id, $this->mini_type);
            });
            $form->text('title')->required();
            $form->list('themes', '话题')->max(5)->required()->saving(function ($v) {
                return implode(',', $v);
            });
            $form->select('tag_id', '标签')->options(Tag::where('type', TagConstant::TAG_BY_TOPIC)->pluck('name', 'id')->all())->required();
            $form->textarea('content')->rules('max:1000', ['max' => '内容不能大于1000个'])->required();
            $form->multipleImage('files')->sortable()->uniqueName()->autoUpload()->saveFullUrl()->saving(function ($v) {
                return implode(',', $v);
            })->customFormat(function ($v) {
                return $v;
            });
            $form->number('relay_count');
            $form->number('fav_count');
            $form->radio('is_hot')->options(['否', '是'])->default(0);
            $form->hidden('mini_type');
            $form->datetime('publish_date')->format('YYYY-MM-DD HH:mm')->required();

            $form->saving(function (Form $form) {
                if (strtotime($form->publish_date) <= time()) {
                    return $form->response()->error('发布时间必须大于当前时间');
                }

                [$member_id, $mini_type] = explode('|', $form->member_id);
                $form->member_id = $member_id;
                $form->mini_type = $mini_type;
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
